(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.fi.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'fiService' ];
	function updateController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, fiService) {
		var ctrl = this, fiInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.fi = {};
		ctrl.fi.fiCodeList = ctrl.fi.fiCodeList || [];

		ctrl.init = function() {
			var params = {
				fiId : fiInfo.fiId
			};
			//add sub data validator
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.fiCode.tableConfigs.table.validatorConfig = ctrl.validatorConfig.fiCodeList;
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.fiCode.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.fi.fiCodeList';
			fiService.initGeneral($scope, ctrl);
			authService.update(params).then(function(response) {
				ctrl.fi = response.data || fiInfo;
				ctrl.fi.fiCodeList = ctrl.fi.fiCodeList || [];
			});
		};

		ctrl.updateSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.fi.taskId) {
				return authService.updateContinueSave(ctrl.fi).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.fi).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.fi.taskId) {
				return authService.updateContinueSubmit(ctrl.fi).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.fi).then(submitSuccessful, submitFailed);
			}
		};

		$scope.$on("COMPANYTYPE2_CANCEL", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});

		$scope.$on("COMPANYTYPE2_OK", function(event, param) {
			event.preventDefault();
			event.stopPropagation();
		});

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedFiCodeList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.fi.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fi.list',
					'state' : 'cda.fi.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.fi = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);